package com.wimcorp.magic.client.input;

import java.util.List;
import java.util.concurrent.Callable;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.lang.StringUtils;
import org.fusesource.jansi.Ansi.Color;

import com.wimcorp.magic.client.CommandLineInterface;
import com.wimcorp.magic.exception.SystemException;

public class ColorSelector extends Selector implements Callable<Integer> {

	private List<String> colorOptions;
	

	public ColorSelector(CommandLineInterface commandLineInterface, List<String> colorOptions) {
		super(commandLineInterface);
		this.colorOptions = colorOptions;
	}

	@Override
	public Integer call() {
		return selectColor("Select Color : ", colorOptions);
	}

	private Integer selectColor(String message, List<String> colorOptions) {
		Options options = getSelectColorOptions();
		Integer result = null;
		CommandLineParser parser = new DefaultParser();
		ConsoleInput selectConsole = new ConsoleInput();
		setRun(true);
		while (isRun()) {
			getCommandLineInterface().getMainConsole().cancel();
			getCommandLineInterface().printInstruction(message);
			try {
				CommandLine line = parser.parse(options, selectConsole.readLine().split(" "));
				for (Option option : line.getOptions()) {
					if ("lc".equals(option.getOpt())) {
						if (colorOptions.size() == 0) {
							for (com.wimcorp.magic.color.Color c : com.wimcorp.magic.color.Color.values()) {
								getCommandLineInterface().print(c.ordinal() + " " + c, Color.WHITE);
							}
						} else {
							for (String color : colorOptions) {
								Color c = Color.valueOf(color);
								getCommandLineInterface().print(c.ordinal() + " " + c, Color.WHITE);
							}
						}
					}
					if ("s".equals(option.getOpt())) {
						String selection = option.getValue();
						if (StringUtils.isNumeric(selection)) {
							Integer colorId = new Integer(selection);
							com.wimcorp.magic.color.Color c = com.wimcorp.magic.color.Color.values()[colorId];
							if (c != null) {
								boolean valid = false;
								for (String color : colorOptions) {
									if (c.getColor().equalsIgnoreCase(color)) {
										valid = true;
									}
								}
								if (valid) {
									setRun(false);
									result = colorId;
								} else {
									throw new SystemException("Selection : " + colorId + " is invalid. ");
								}
							} else {
								throw new SystemException("No color with id : " + colorId + " found. ");
							}
						}
					}
				}
				processCommands(options, line.getOptions());
			} catch (Exception e) {
				getCommandLineInterface().print(e.getMessage(), Color.RED);
			}

		}
		return result;
	}
	
	public Options getSelectColorOptions() {
		Options options = new Options();
		options.addOption(getHelpOption());
		options.addOption(getListColorsOption());
		options.addOption(getPlayerListOption());
		options.addOption(getSelectColorOption());
		options.addOption(getViewBattleFieldOption());
		options.addOption(getViewHandOption());
		options.addOption(getViewLifeOption());
		return options;
	}

}
